/**
 * 
 */
package Control;

import javax.swing.JOptionPane;

import Entity.MsgPackage;
import Entity.User;
import GUI.ScreenManager;
import GUI.UpdateWorkerDetails_1;
import GUI.UpdatePassword;

/**
 * This class update worker private details or update his password to system and 
 * send the given information from the GUI's class to the server
 * @author aviat
 *
 */
public class UpdateWorkerDetailsControl {
	
	private UpdatePassword updateWpassword;
	private User user;
	private MsgPackage workerData;
	private UpdateWorkerDetails_1 updateWdetails;
	
	/**
	 * Constructor
	 *
	 */
	public UpdateWorkerDetailsControl() {
		updateWpassword = new UpdatePassword();
		user = new User();
		workerData = new MsgPackage();
		updateWdetails =new UpdateWorkerDetails_1();
		
	}
	
	/**
	 * This method sent a request to server to change worker password in DB
	 *@return true if update password was a success and false otherwise 
	 */
	public boolean changeWorkerPassword(){
		if(!(updateWpassword.getuName().getText().toString().equals(""))){
			System.out.println(updateWpassword.getuName().toString());
			String pass = updateWpassword.getPass().getText().toString();
			String Vpass = updateWpassword.getVerPass().getText().toString();
			if(pass.equals(null) || Vpass.equals(null)){
				JOptionPane.showMessageDialog(null, "You need to fill all fields ", "ERROR", JOptionPane.ERROR_MESSAGE);
				return false;
			}
			else {
				if(pass.equals(Vpass)){
					user.setUserName(Integer.parseInt(updateWpassword.getuName().getText().toString()));
					user.setPassword(updateWpassword.getPass().getText().toString());
					workerData.setObject(user);
					workerData.setOp_code(OP_CODE.UPDATE_WORKER_PASSWORD);
					ScreenManager.getConnection().accept(workerData);
					
					Object o = ScreenManager.getConnection().cclient.getMessage();
	
					if((boolean) o){
						JOptionPane.showMessageDialog(null, "Password changed successfully ", "", JOptionPane.INFORMATION_MESSAGE);
					}
					else{
						JOptionPane.showMessageDialog(null, "User name doesn't exist ", "ERROR", JOptionPane.ERROR_MESSAGE);
						return false;
					}
				}
					
				else{
					JOptionPane.showMessageDialog(null, "Password verification didn't succeed, Enter password again ", "ERROR", JOptionPane.ERROR_MESSAGE);
					return false;
				}
			}
		}
		else{
			JOptionPane.showMessageDialog(null, "You need to enter user name", "ERROR", JOptionPane.ERROR_MESSAGE);
			return false;
		}
		
		return true;
	}
	
	
	/**		
			 * This method sent a request to server to update worker private details in DB		
			 *@return true if update details was a success and false otherwise		
			 */		
	public void viewWorkerDetails(){		
		System.out.println("in update");		
				
		System.out.println("pass not 000");		
		workerData.setObject(ScreenManager.getUserLogged().getUserName());		
		workerData.setOp_code(OP_CODE.VIEW_WORKER_DETAILS);		
		ScreenManager.getConnection().accept(workerData);		
				
		Object ud = ScreenManager.getConnection().cclient.getMessage();		
		User u = (User) ud;		
				
		updateWdetails.getUserNameTextField().setText(u.getName());		
		updateWdetails.getLastNameTextField().setText(u.getLName());		
		updateWdetails.getEmailAddressTextField().setText(u.getEmail());		
		updateWdetails.getHomeAddressTextArea().setText(u.getAddress());		
		updateWdetails.getPhoneNumberTextField().setText(u.getPhoneNum());			
		}		
		
	/**
	 * This method sent a request to server to update worker private details in DB
	 *@return true if update details was a success and false otherwise 
	 */
	public boolean updateWorkerData(){
		if(updateWdetails.getUserNameTextField().getText()==null || updateWdetails.getLastNameTextField().getText()==null ||
				updateWdetails.getPhoneNumberTextField().getText()==null || updateWdetails.getEmailAddressTextField().getText()==null ||
				updateWdetails.getHomeAddressTextArea().getText()==null){
			JOptionPane.showMessageDialog(null, "Some fields are empty, Please fill all fieldes... ", "ERROR", JOptionPane.ERROR_MESSAGE);
			return false;
		}
		else if(updateWdetails.getPhoneNumberTextField().getText().toString().length()!=10){
			JOptionPane.showMessageDialog(null, "Phone number must be 10 digits... ", "ERROR", JOptionPane.ERROR_MESSAGE);
			return false;
		}
		else{
			user.setUserName(Integer.parseInt(String.valueOf(ScreenManager.getUserLogged().getUserName())));
			System.out.println(user.getUserName());
			
			user.setName(updateWdetails.getUserNameTextField().getText().toString());
			user.setLName(updateWdetails.getLastNameTextField().getText().toString());
			user.setEmail(updateWdetails.getEmailAddressTextField().getText().toString());
			user.setAddress( updateWdetails.getHomeAddressTextArea().getText().toString());
			//user.setAge(chData.getAgeTextField().getText().toString());
			user.setPhoneNum(updateWdetails.getPhoneNumberTextField().getText());
			workerData.setObject(user);
			workerData.setOp_code(OP_CODE.UPDATE_WORKER_DETAILS);
			ScreenManager.getConnection().accept(workerData);
				
			System.out.println("back from DB");
			Object o = ScreenManager.getConnection().cclient.getMessage();
			if((boolean) o){
				JOptionPane.showMessageDialog(null, "Your Details has been updated successfully ", "", JOptionPane.INFORMATION_MESSAGE);
			}
		}
		return true;
	}
	/**
	 * @return the updateWpassword
	 */
	public UpdatePassword getUpdateWpassword() {
		return updateWpassword;
	}
	/**
	 * @param updateWpassword the updateWpassword to set
	 */
	public void setUpdateWpassword(UpdatePassword updateWpassword) {
		this.updateWpassword = updateWpassword;
	}
	/**
	 * @return the user
	 */
	public User getUser() {
		return user;
	}
	/**
	 * @param user the user to set
	 */
	public void setUser(User user) {
		this.user = user;
	}
	/**
	 * @return the workerData
	 */
	public MsgPackage getWorkerData() {
		return workerData;
	}
	/**
	 * @param workerData the workerData to set
	 */
	public void setWorkerData(MsgPackage workerData) {
		this.workerData = workerData;
	}
	/**
	 * @return the chData
	 */
	public UpdateWorkerDetails_1 getUpdateWdetails() {
		return updateWdetails;
	}
	/**
	 * @param chData the chData to set
	 */
	public void setUpdateWdetails(UpdateWorkerDetails_1 upfateWdetails) {
		this.updateWdetails = upfateWdetails;
	}
}
